package org.snail.attendence.controller;

import org.apache.log4j.Logger;
import org.snail.attendence.bean.WxLeaveDO;
import org.snail.attendence.bean.WxleanveCondition;
import org.snail.attendence.service.impl.AttendanceFileOutputServiceImpl;
import org.snail.attendence.service.impl.WxLeaveServiceImpl;
import org.snail.constant.ErrCodeConstant;
import org.snail.exception.CommonException;
import org.snail.exception.JsonResult;
import org.snail.other.service.impl.SendMessageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/wxLeave")
public class WxLeaveController {
    private final Logger logger = Logger.getLogger(WxLeaveController.class);
    @Autowired
    private WxLeaveServiceImpl wxLeaveService;
    @Autowired
    private SendMessageServiceImpl sendMessageService;
    @Autowired
    private AttendanceFileOutputServiceImpl attendanceFileOutputService;


    /**
     * 微信考勤预览
     *
     * @param file
     * @param request
     * @return
     * @throws CommonException
     */
    @RequestMapping("/previewAttendenceWX")
    @ResponseBody
    public JsonResult prevWxAttendance(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws CommonException {
        JsonResult jsonResult = new JsonResult();
        List<WxLeaveDO> wxLeaveDOList = wxLeaveService.previewAttendenceWX(file, request);
        jsonResult.setData(wxLeaveDOList);
        return jsonResult;
    }

    /**
     * 微信考勤保存
     * @param leaveDOs
     * @return
     * @throws CommonException
     */
    @RequestMapping("/saveAttendenceWX")
    @ResponseBody
    public JsonResult getAbnormalLeave(@RequestBody List<WxLeaveDO> leaveDOs) throws CommonException {
        JsonResult jsonResult = new JsonResult();
        wxLeaveService.saveAttendenceWX(leaveDOs);
        jsonResult.setCode(ErrCodeConstant.SUCCESS);
        jsonResult.setMessage("微信考勤保存成功");
        return jsonResult;
    }

    /**
     * 获取微信考勤的文件
     * @return
     */
    @RequestMapping("/findWXFile")
    @ResponseBody
    public JsonResult findWXFile( HttpServletRequest request){
        JsonResult jsonResult = new JsonResult();
        List list = wxLeaveService.findWXFile(request);
        jsonResult.setData(list);
        return jsonResult;
    }

    /**
     * 微信详情
     * @param wxleanveCondition
     * @return
     */
    @RequestMapping("/DetailsLetter")
    @ResponseBody
    public JsonResult detailsLetter(WxleanveCondition wxleanveCondition){
        JsonResult jsonResult = new JsonResult();
        List<WxLeaveDO> wxLeaveDOs = wxLeaveService.findDetails(wxleanveCondition);
        jsonResult.setData(wxLeaveDOs);
        jsonResult.setMessage("微信详情查询成功");
        return jsonResult;
    }

    @RequestMapping("/attendanceDownLoad")
    public ResponseEntity<byte[]> download(@RequestParam("fileName") String fileName,HttpServletRequest request) throws IOException, CommonException {
        return wxLeaveService.wxLeaveDownload(fileName,request);
    }
}
